﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Common;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Purchase;
using PeiYue.IServices.Store;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Models.Store;
using PeiYue.Services.Store;

namespace PeiYue.Api.Controllers.Store
{
    /// <summary>
    /// 仪表盘  数据统计
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DashboardController : ControllerBase
    {
        readonly IPickProgressServices _pickProgressServices;
        readonly IReceiptRecordServices _receiptRecordServices;
        readonly ISrmSortingRecord _srmSortingRecord;
        readonly IDriverScanLogServices _driverScanLogServices;
        readonly IDeliveryServices _deliveryServices;
        readonly IUser _user;
        public DashboardController(
            IPickProgressServices pickProgressServices,
            IReceiptRecordServices receiptRecordServices,
            ISrmSortingRecord srmSortingRecord,
            IDriverScanLogServices driverScanLogServices,
            IDeliveryServices deliveryServices,
            IUser user)
        {
            _pickProgressServices = pickProgressServices;
            _receiptRecordServices = receiptRecordServices;
            _srmSortingRecord = srmSortingRecord;
            _driverScanLogServices = driverScanLogServices;
            _deliveryServices = deliveryServices;
            _user = user;
        }

        /// <summary>
        ///  仪表盘 数据统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<DashboardDto>> GetDashboard()
        {
            DashboardDto dto = new DashboardDto();
            List<int> timeList = new List<int>() { 21, 22, 23, 0, 1, 2, 3, 4, 5, 6, 7 };
            //var pickList = new List<PickProgressDao>();
            var nowDate = DateTime.Now.Date;
            var yestodayDate = DateTime.Now.Date.AddDays(-1);
            var tomorrowDate = DateTime.Now.Date.AddDays(1);
            var queryDate = new DateTime();
            var loadQueryDate = new DateTime();
            bool isNow = false;
            if (DateTime.Now.Hour >= int.Parse(Appsettings.app(new string[] { "QueryHour", "ReceiptHour" })))
            {
                loadQueryDate = tomorrowDate;
                queryDate = new DateTime(nowDate.Year, nowDate.Month, nowDate.Day, 21, 0, 0);
                isNow = true;
               // pickList = await _pickProgressServices.Query(t => t.预计发货 >= tomorrowDate);
            }
            else
            {
                loadQueryDate = nowDate;
                queryDate = new DateTime(yestodayDate.Year, yestodayDate.Month, yestodayDate.Day, 21, 0, 0);
                isNow = false;
               // pickList = await _pickProgressServices.Query(t => t.预计发货 >= nowDate && t.预计发货 < tomorrowDate);
            }
            dto.OrderAmount = await _pickProgressServices.GetOrderAmount(isNow);
            dto.SalesAmount =await _pickProgressServices.GetSalesAmount(isNow);
            var pickList = await _pickProgressServices.GetPickPickProgresList(isNow);
            dto.PackTotal = pickList.Count;
            dto.SelfPickPackTotal = pickList.Where(t=>t.分拣方式=="自行分拣" && t.客户编号!= "A1213").Count();
            //已分拣自拣包裹数列表
            var selfPickPackList = pickList.Where(t => t.分拣方式 == "自行分拣" && (t.拣选数.HasValue && t.拣选数.Value > 0)).ToList();
            dto.SelfPickPackCount = selfPickPackList.Count;
            //dto.ReceivingPackTotal = pickList.Where(t => t.分拣方式 == "供应商分拣").Count();
            dto.ReceivingPackTotal =await _pickProgressServices.GetSupplierSortTotal(isNow);
            //已收货自拣包裹数列表
            var receivingPackList = await _receiptRecordServices.Query(t => t.UpdateTime >= queryDate && t.Status == 1 && t.ReceiptQty > 0);
            dto.ReceivingPackCount = receivingPackList.Count;
            dto.LoadingPackTotal = dto.PackTotal;
            var loadingPackList = await _driverScanLogServices.Query(t => t.create_time >= loadQueryDate && t.status == 1);
            dto.LoadingPackCount =loadingPackList.Count ;
            //dto.DeliveryPackTotal = pickList.Where(t =>(t.拣选数.HasValue && t.拣选数.Value > 0)).Count();
            dto.DeliveryPackTotal = dto.SelfPickPackCount + dto.ReceivingPackCount;//待投递包裹数=已分拣+已收货
            //已投递包裹数列表
            var deliveryPackList =await _deliveryServices.Query(t => t.CreateTime >= queryDate && t.Status == 0);
            dto.DeliveryPackCount = deliveryPackList.Count;
            int itemCount = 0;
            var selfPickPackGroupList = selfPickPackList.OrderBy(t => t.分拣日期).GroupBy(t => (t.分拣日期.HasValue ? t.分拣日期.Value.Hour:22)).Take(11).ToList();
            //已分拣
            foreach (var time in timeList)
            {
                foreach (var item in selfPickPackGroupList)
                {
                    if (time == item.Key)
                    {
                        itemCount += item.Count();
                    }
                }
                dto.SelfPickList.Add(itemCount);
            }
            //已收货
            itemCount = 0;
            var receivingPackGroupList = receivingPackList.OrderBy(t => t.UpdateTime).Where(t => t.UpdateTime >= queryDate).GroupBy(t => t.UpdateTime.Hour).Take(11).ToList();
            foreach (var time in timeList)
            {
                foreach (var item in receivingPackGroupList)
                {
                    if (time == item.Key)
                    {
                        itemCount += item.Count();
                    }
                }
                dto.ReceivingPackList.Add(itemCount);
            }
            
            //已投递
            itemCount = 0;
            var deliveryPackGroupList = deliveryPackList.OrderBy(t => t.CreateTime).Where(t => t.CreateTime >= queryDate).GroupBy(t => t.CreateTime.Hour).Take(11).ToList();
            foreach (var time in timeList)
            {
                foreach (var item in deliveryPackGroupList)
                {
                    if (time == item.Key)
                    {
                        itemCount += item.Count();
                    }
                }
                dto.DeliveryPackList.Add(itemCount);
            }
             
            //已装车
            itemCount = 0;
            var loadingPackGroupList = loadingPackList.OrderBy(t => t.create_time).Where(t => t.create_time >= queryDate).GroupBy(t => t.create_time.Value.Hour).Take(11).ToList();
            foreach (var time in timeList)
            {
                foreach (var item in loadingPackGroupList)
                {
                    if (time == item.Key)
                    {
                        itemCount += item.Count();
                    }
                }
                dto.LoadingPackList.Add(itemCount);
            }
            return new MessageModel<DashboardDto>()
            {
                msg = "获取成功",
                success = true,
                response = dto
            };
        }
    }
    public class DashboardDto
    {
        /// <summary>
        /// 订单金额
        /// </summary>
        public decimal OrderAmount { get; set; }
        /// <summary>
        /// 销售金额
        /// </summary>
        public int SalesAmount { get; set; }
        /// <summary>
        /// 今日总包裹数
        /// </summary>
        public int PackTotal { get; set; }
        /// <summary>
        /// 自拣总包裹数
        /// </summary>
        public int SelfPickPackTotal { get; set; }
        /// <summary>
        /// 已分拣自拣包裹数
        /// </summary>
        public int SelfPickPackCount { get; set; }
        /// <summary>
        /// 收货总包裹数
        /// </summary>
        public int ReceivingPackTotal { get; set; }
        /// <summary>
        /// 已收货包裹数
        /// </summary>
        public int ReceivingPackCount { get; set; }
        /// <summary>
        /// 投递总包裹数
        /// </summary>
        public int DeliveryPackTotal { get; set; }
        /// <summary>
        /// 已投递包裹数
        /// </summary>
        public int DeliveryPackCount { get; set; }
        /// <summary>
        /// 装车总包裹数
        /// </summary>
        public int LoadingPackTotal { get; set; }
        /// <summary>
        /// 已装车包裹数
        /// </summary>
        public int LoadingPackCount { get; set; }
        /// <summary>
        /// 自拣 折线图数组
        /// </summary>
        public List<int> SelfPickList { get; set; } = new List<int>();
        /// <summary>
        /// 收货 折线图数组
        /// </summary>
        public List<int> ReceivingPackList { get; set; } = new List<int>();
        /// <summary>
        /// 已投递 折线图数组
        /// </summary>
        public List<int> DeliveryPackList { get; set; } = new List<int>();
        /// <summary>
        /// 已装车 折线图数组
        /// </summary>
        public List<int> LoadingPackList { get; set; } = new List<int>() ;
    }
}